package org.duomn.replace;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.duomn.replace.util.FileUtils;

/**
 * Hello world!
 * 
 */
public class App {

	private static Logger log = Logger.getLogger("replace");

	public static void main(String[] args) {
		log.setLevel(Level.INFO);

//		String a = "  <fileSet encoding=\"UTF-8\">\r\n  "
//				+ "  <directory>.settings</directory>\r\n" + "<includes>\r\n"
//				+ "<include>**/*.prefs</include>\r\n"
//				+ "<include>**/*.component</include>\r\n"
//				+ "<include>**/*.container</include>\r\n"
//				+ "<include>**/*.name</include>\r\n"
//				+ "<include>**/*.jsdtscope</include>\r\n" + "</includes>\r\n"
//				+ "</fileSet>\r\n";
//		String c = a.replaceAll(
//					"([\\r\\n\\t\\s]*<fileSet encoding=\"UTF-8\">"
//					+ "[\\r\\n\\t\\s]*<directory>.settings</directory>"
//					+ "[\\r\\n\\t\\s]*<includes>"
//					+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.prefs</include>)"
//					+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.component</include>"
//					+ "([\\r\\n\\t\\s]*<include>\\*\\*/\\*.container</include>"
//					+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.name</include>"
//					+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.jsdtscope</include>"
//					+ "[\\r\\n\\t\\s]*</includes>"
//					+ "[\\r\\n\\t\\s]*</fileSet>)",
//				"$1$2");
//
//		System.out.println(c);
//		String a1 = "aacckacgbbcc";
//		String b = a1.replaceAll("(k)ac(g)", "$1,$2");
//		System.out.println(b);

//		File file = new File("D:/Git/springbase/springmybatis");
//		File file = new File("D:/Git/springbase/springbase/target/generated-sources/archetype/src/main/resources/META-INF/maven/archetype-metadata.xml");
		
		File file = new File(args[0]);
		FileUtils.hackListFiles(file, new FileUtils.Hackable() {
			public void hack(File file) {
				String name = file.getPath();
				if (name.indexOf("target") == -1)
					return;

				if (name.endsWith("archetype-resources/pom.xml")
						|| name.endsWith("archetype-resources\\pom.xml")) {
					log.info("file:" + name);
					String content = FileUtils.readFile(file);
					content = content.replaceAll(
							"<finalName>[^<]*</finalName>",
							"<finalName>\\${artifactId}</finalName>");
					FileUtils.writeToFile(file, content);
				} else if (name.endsWith("archetype-metadata.xml")) {
					log.info("file:" + name);
					String content = FileUtils.readFile(file);
					content = content
							.replaceAll(
									"([\\r\\n\\t\\s]*<fileSet filtered=\"true\" encoding=\"UTF-8\">"
											+ "[\\r\\n\\t\\s]*<directory>.settings</directory>"
											+ "[\\r\\n\\t\\s]*<includes>"
											+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.xml</include>)"
											+ "([\\r\\n\\t\\s]*)(</includes>"
											+ "[\\r\\n\\t\\s]*</fileSet>)",
									"$1$2<include>**/*.component</include>$2$3")
							.replaceAll(
									"([\\r\\n\\t\\s]*<fileSet encoding=\"UTF-8\">"
											+ "[\\r\\n\\t\\s]*<directory>.settings</directory>"
											+ "[\\r\\n\\t\\s]*<includes>"
											+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.prefs</include>)"
											+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.component</include>"
											+ "([\\r\\n\\t\\s]*<include>\\*\\*/\\*.container</include>"
											+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.name</include>"
											+ "[\\r\\n\\t\\s]*<include>\\*\\*/\\*.jsdtscope</include>"
											+ "[\\r\\n\\t\\s]*</includes>"
											+ "[\\r\\n\\t\\s]*</fileSet>)",
									"$1$2");
					FileUtils.writeToFile(file, content);
				} else if (name.endsWith(".xml") || name.endsWith(".classpath")
						|| name.endsWith(".project")) {
					log.info("file:" + name);
					String content = FileUtils.readFile(file);
					content = content
							.replace(
									"<?xml version=\"${version}\" encoding=\"UTF-8\"?>",
									"<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
							.replace(
									"facet=\"wst.jsdt.web\" version=\"${version}\"",
									"facet=\"wst.jsdt.web\" version=\"1.0\"");
					FileUtils.writeToFile(file, content);
				} else if (name.endsWith(".component")) {
					log.info("file:" + name);
					String content = FileUtils.readFile(file);
					content = content
							.replaceAll("<wb-module deploy-name=\"[^>]*\">",
									"<wb-module deploy-name=\"\\${artifactId}\">")
							.replaceAll(
									"<property name=\"java-output-path\" value=\"/[^>]*/target/classes\"",
									"<property name=\"java-output-path\" value=\"/\\${artifactId}/target/classes\"")
							.replaceAll(
									"<property name=\"context-root\" value=\"[^>]*\"/>",
									"<property name=\"context-root\" value=\"\\${artifactId}\"/>");
					FileUtils.writeToFile(file, content);
				}
			}
		});
		System.out.println("Hello World!");
	}
}
